// 设置 localStorage
export const setLocalStorage = <T>(key: string, value: T): void => {
  try {
    const serializedValue = JSON.stringify(value);
    localStorage.setItem(key, serializedValue);
  } catch (error) {
    console.error('设置 localStorage 失败:', error);
  }
};

// 获取 localStorage
export const getLocalStorage = <T>(key: string, defaultValue: T | null = null): T | null => {
  try {
    const item = localStorage.getItem(key);
    if (item === null) {
      return defaultValue;
    }
    return JSON.parse(item);
  } catch (error) {
    console.error('获取 localStorage 失败:', error);
    return defaultValue;
  }
};

// 删除指定项
export const removeLocalStorage = (key: string): void => {
  try {
    localStorage.removeItem(key);
  } catch (error) {
    console.error('删除 localStorage 失败:', error);
  }
};

// 清空所有 localStorage
export const clearLocalStorage = (): void => {
  try {
    localStorage.clear();
  } catch (error) {
    console.error('清空 localStorage 失败:', error);
  }
};
